package com.cop4331.social;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 
 * @author Matt Szoke
 * @LastUpdated: 4/12/2012 by Matt Szoke
 * 
 * Asks user for information to prompt SecurityActivity
 * 
 */
public class ForgotpassActivity extends Activity {

	Button proceed;
	Button cancel;
	EditText un;
	TextView err;

	// -----------------------------------------------
	// ONCREATE
	// -----------------------------------------------
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		// Attach to xml
		setContentView(R.layout.forgotpass);

		// Buttons
		proceed = (Button) findViewById(R.id.b_proceed);
		cancel = (Button) findViewById(R.id.b_cancel);

		// EditText
		un = (EditText) findViewById(R.id.et_user); // TextField for Username

		// TextView
		err = (TextView) findViewById(R.id.err); // TextView shows error to user

		// Gives functionality to "PROCEED" button
		proceed.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Retreives username from EditText Field
				String username = un.getText().toString();

				UserFunctions userFunction = new UserFunctions();

				// Check that user exists
				JSONObject json = userFunction.userExists(username);

				try {
					if (json.getString("success") != null) {

						String res = json.getString("success");

						// If user exists
						if (Integer.parseInt(res) == 1) {

							// Prepare bundle for Security Activity
							Bundle b = new Bundle();
							err.setText("");

							// Retreive user
							JSONObject json_user = json.getJSONObject("user");

							// Bundle username, password, security question,
							// security answer
							String user = json_user.getString("username");
							String pass = json_user.getString("password");
							String secur = json_user.getString("sec");
							String ans = json_user.getString("ans");

							b.putString("username", user);
							b.putString("sec", secur);
							b.putString("ans", ans);
							b.putString("pass", pass);
							b.putString("email", json_user.getString("email"));
							b.putString("full", json_user.getString("full"));

							// Add extras and continue to SecurityActivity
							Intent sec = new Intent(getApplicationContext(),
									SecurityActivity.class);
							sec.putExtras(b);
							startActivity(sec);
							finish();
						}

						// Username entered does not exist
						else
							err.setText("User does not exist.");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});

		// Gives functionality to "CANCEL" button
		cancel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Return to LoginActivity
				Intent log = new Intent(getApplicationContext(),
						LoginActivity.class);
				startActivity(log);
				finish();

			}
		});
	}
}
